# -*- coding: utf-8 -*-

from w.blocks.containers.block import Block

from w.html.doctypes.preferable import Ul,Li

from w.utils.dobject.strict import DynamicInit

from w.javascript.base import JSTranslator

class UList(Block):
    generateLiIDs = True
    
    cssFiles = ['/w/blocks/ulist.css']

    def __init__(self, content=None, *argv, **kwargv):
        Block.__init__(self, content)

        self.rules = DynamicInit(Li={})

        self.tag = Ul(*argv, **kwargv)
        self.html = self.tag.html
    
    def generateLiID(self, seed):
        return '%srow%s'%(self.blockID, seed)
    
    def getSeedFromID(self, ID):
        return ID.split('row')[1]
    
    def ruledLi(self, li):
        d = {'self': li, 'ID': li.html.id, 'index': self.getSeedFromID(li.html.id)}
	
        for name,value in self.rules.Li.items():
            if callable(value) and not isinstance(value, JSTranslator):
                setattr(li.html, name, value(**d))
            else:
                setattr(li.html, name, value%d)
        return li

    def createLi(self, rowNum, content=None, head=False):
        li = Li(content,hid=self.generateLiID(rowNum)) if self.generateLiIDs else Li()
        return self.ruledLi(li)

    def make(self, content=[]):
        if not content:
            content = self

        self.tag.extend([self.createLi(i, content[i]) for i in range(len(content))])
        return Block.make(self, [self.tag])
